//
//  BHImageOrVideoItemCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/6.
//

import UIKit

class BHImageOrVideoItemCell: UICollectionViewCell {
    
    var addOrClearImageActBlock:(() -> Void)?
    var videoPlayOrPauseActBlock:((Bool) -> Void)?
    
    @IBOutlet weak var smallImgView: UIImageView!
    
    @IBOutlet weak var choseSignBtn: UIButton!
    
    @IBOutlet weak var time_label: UILabel!
    
    
    var chose: Bool = false {
        didSet {
            if chose {
                self.choseSignBtn.backgroundColor = .color_34CE98
            }else {
                self.choseSignBtn.backgroundColor = .clear
            }
        }
    }
    
    // 什么模式 0 默认缩略图  1预览
    var type: Int = 0 
    
    var model: BHImageOrVideoModel! {
        didSet {
            if model.asset != nil {
                if self.type == 0 {
                    
                    model.transAssetToImage {[weak self] (img) in
                        if img == nil {
                            return
                        }
                        self?.smallImgView.image = img
                    }
                   
                    self.choseSignBtn.isHidden = false
                }else {
                    model.transAssetToImage {[weak self] (img) in
                        self?.smallImgView.image = img
                    }
                    self.choseSignBtn.isHidden = true
                }
                
                if model.asset!.mediaType == .image {
                    self.time_label.isHidden = true
                }else {
                    self.time_label.isHidden = false
                    self.time_label.text = model.durationTime
                }
                
                if model.index != 0 {
                    self.choseSignBtn.setTitle("\(model.index)", for: .normal)
                    self.chose = true
                }else {
                    self.choseSignBtn.setTitle("", for: .normal)
                    self.chose = false
                }

            }
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.choseSignBtn.addLayerCorner(9, 0, .white)
        self.choseSignBtn.enlargeEdgeInset = UIEdgeInsets(top: 5, left: 10, bottom: 10, right: 5)
    }
    
    @IBAction func choseImageAct(_ sender: UIButton) {
        self.addOrClearImageActBlock?()
    }
    
    
    @IBAction func videoPlayAct(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        
        // true为暂停  false为播放
        self.videoPlayOrPauseActBlock?(sender.isSelected)
    }
    
}
